package com.paddyi.controller;

import com.paddyi.commons.http.HttpResult;
import com.paddyi.model.Organization;
import com.paddyi.service.IOrganizationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 组织机构 (Organization)
 *
 * @author paddy
 * @since 2019-04-26 04:34:10
 */
@RestController
@RequestMapping("organization")
@Api(tags = "组织机构接口", description = "组织机构controller")
public class OrganizationController extends BaseController {

    @Autowired
    private IOrganizationService organizationService;

    /**
     * 保存组织机构
     *
     * @param record
     * @return
     */
    @ApiOperation(value = "添加", notes = "添加请求")
    @PostMapping(value = "/add")
    public HttpResult add(@RequestBody Organization record) {
        Long userId = getUserId();
        Date date = new Date();
        record.setCreateUserId(userId);
        record.setCreateTime(date);
        record.setUpdateUserId(userId);
        record.setUpdateTime(date);
        return HttpResult.ok(organizationService.save(record));
    }

    /**
     * 修改组织机构
     *
     * @param record
     * @return
     */
    @PostMapping(value = "/edit")
    public HttpResult edit(@RequestBody Organization record) {
        record.setUpdateTime(new Date());
        record.setUpdateUserId(getUserId());
        return HttpResult.ok(organizationService.updateById(record));
    }

    /**
     * 删除组织机构
     *
     * @param records
     * @return
     */
    @PostMapping(value = "/delete")
    public HttpResult delete(@RequestBody List<Organization> records) {
        List list = new ArrayList();
        for(Organization or : records){
            list.add(or.getId());
        }
        return HttpResult.ok(organizationService.removeByIds(list));
    }

    /**
     * 基础分页查询,条件查询只支持等于，如果想要更高级的功能，比如大于小于等等，请自己写吧
     *
     * @return
     */
    @PostMapping (value = "/findPage")
    public HttpResult findPage() {

        return HttpResult.ok(organizationService.findTree());
    }

    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/findById")
    public HttpResult findById(@RequestParam Long id) {
        return HttpResult.ok(organizationService.getById(id));
    }
}
